<?php
/**
 * Created by PhpStorm.
 * Author: 芸签cms www.yunqiancms.com
 * User: sun
 * Date: 2022/6/27
 * Time: 3:47 PM
 */
namespace app\common\models;

use app\common\exceptions\BizException;

class Article extends BaseModel
{
    public $table = 'yq_article';
    public $timestamps = true;
    protected $guarded = [''];

    public function parent()
    {
        return $this->hasOne(self::class, 'id', 'pid');
    }
    public function children()
    {
        return $this->hasMany(self::class, "pid",'id');
    }
    //
    public function tags()
    {
        return $this->belongsToMany(Tag::class,'yq_article_tag','article_id','tag_id');
    }
    public function category()
    {
        return $this->hasOne(Category::class, 'id', 'cid');
    }
    /**
     *
     * @param $params
     * $params=[
     * 'select'=>[字段1，字段2] 或空
     * 'where'=>sql条件字符串,
     * 'order'=>sql排序字符串
     * 'limit'=>sql的limit,
     * 'page_size'=>数字
     * ]
     * @return Category
     * @throws BizException
     */
    public static function getListByCondition($params){
        $query = self::site();
        if($params['select']){
            $query = $query->select($params['select']);
        }else{
            $query = $query->select(['id','cid','title','keywords','description','author','publish_time','source','thumb','links','is_recommend','is_top']);
        }

        $query = $query->with(['category' => function($query) {
            return $query->select();
        }]);
        $query = $query->where('is_publish',self::STATUS_NORMAL);
        if($params['where']){
            $query = $query->whereRaw($params['where']);
        }
        if($params['order']){
            $query = $query->orderByRaw($params['order']);
        }
        if($params['limit'] && $params['page_size']){
            throw new BizException('limit和page_size只能传其中一个');
        }
        if($params['limit']){
            $limit = explode(',',$params['limit']);
            if(count($limit)==2){
                $query = $query
                    ->offset(intval($limit[0]))
                    ->limit(intval($limit[1]));
            }else{
                $query = $query
                    ->limit(intval($params['limit']));
            }
        }
        if($params['page_size']){
            $query = $query
                ->paginate($params['page_size']);
        }else{
            $query = $query->get();
        }
        $query->map(function ($v){
            $v->cname = '';
            if($v['category']&&$v['category']['name']){
                $v->cname = $v['category']['name'];
            }
            $v->publish_time = date('Y-m-d H:i:s',$v->publish_time);
            $v->thumb = fmt_media_url($v->thumb);
            unset($v->category);
        });
        return $query;
    }
    public  function atributeNames() {
        return [
            'cid'  => '分类',
            'title' => '标题'
        ];
    }

    public  function rules()
    {
        return [
            'cid'  => 'required',
            'title' => 'required'
        ];
    }
}